package com.ebanma.cloud.user.strategy;

import com.ebanma.cloud.exception.BusinessException;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class GranterHolder {
    private final Map<String, LoginStrategy> granterMap = new ConcurrentHashMap<>();

    public GranterHolder(Map<String, LoginStrategy> granterMap) {
        this.granterMap.putAll(granterMap);
    }

    /**
     * 根据登陆类型获取具体的处理类
     * @param grantType
     * @return
     */
    public LoginStrategy getGranter(String grantType) {
        LoginStrategy loginStrategy = granterMap.get(grantType);
        Optional.ofNullable(loginStrategy).orElseThrow(() -> new BusinessException("不存在该登陆类型"));
        return loginStrategy;
    }

}
